//
//  HWMeViewController.m
//  HWNetEase
//
//  Created by lanou on 16/4/5.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWMeViewController.h"
#import "HWMeTableViewCell.h"
#import "HWMeFootView.h"
#import "HWLoginViewController.h"

@interface HWMeViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UIButton *settingBtn;

@property (nonatomic, strong)UIButton *moonBtn;

@property (nonatomic, strong)UITableView *tableView;

@end

@implementation HWMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNavBar];
    [self setupTableView];
    
}

#pragma mark --- 设置导航条 ---
- (void)setupNavBar
{
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc]initWithCustomView:self.settingBtn], [[UIBarButtonItem alloc]initWithCustomView:self.moonBtn]];
}

- (UIButton *)settingBtn
{
    if (_settingBtn == nil)
    {
        _settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_settingBtn setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal];
        [_settingBtn setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted];
        _settingBtn.size = _settingBtn.currentBackgroundImage.size;
    }
    return _settingBtn;
}

- (UIButton *)moonBtn
{
    if (_moonBtn == nil)
    {
        _moonBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moonBtn setBackgroundImage:[UIImage imageNamed:@"mine-moon-icon"] forState:UIControlStateNormal];
        [_moonBtn setBackgroundImage:[UIImage imageNamed:@"mine-moon-icon-click"] forState:UIControlStateHighlighted];
        _moonBtn.size = _moonBtn.currentBackgroundImage.size;
    }
    
    return _moonBtn;
}

#pragma mark --- 创建tableView ---
- (void)setupTableView
{
    self.tableView.backgroundColor = BackgroundColor;
    [self.tableView registerClass:[HWMeTableViewCell class] forCellReuseIdentifier:@"loading"];
    
    // 调整header和footer
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 10;
    
    // 调整inset
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    
    // 将自定义footer初始化出来
    self.tableView.tableFooterView = [[HWMeFootView alloc]init];
    
    //frame
    [[RACObserve(self.tableView.tableFooterView, frame) distinctUntilChanged] subscribeNext:^(id x) {
        self.tableView.tableFooterView = self.tableView.tableFooterView;
    }];
    
    [self.view addSubview:_tableView];
}

- (UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        // 设置滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWMeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"loading"];
    if (indexPath.section == 0)
    {
        cell.imageView.image = [UIImage imageNamed:@"setup-head-default"];
        cell.textLabel.text = @"登录/注册";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        cell.textLabel.text = @"离线下载";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        HWLoginViewController *loginVc = [[HWLoginViewController alloc]init];
        [self presentViewController:loginVc animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
